from django.db import models
from django.contrib.auth.models import User
from datetime import datetime, timedelta
import random, string
import os
from django.contrib.contenttypes.models import ContentType
from extfunctions import slugify
class Message(models.Model):
	sender = models.ForeignKey(User, related_name="fbla_sender")
	reciever = models.ForeignKey(User, related_name="fbla_reciever")
	subject = models.CharField(max_length=100)
	content = models.TextField()
	date_sent = models.DateTimeField(default=datetime.now())
	replied_status = models.BooleanField(default=False)
	viewed = models.BooleanField(default=False)
	def __unicode__(self):
		return self.subject
	
def img_path(instance, filename):
	ctype = ContentType.objects.get_for_model(instance)
	extension = filename.split('.')[-1]
	directory = "usergallery"
	chars = string.letters + string.digits
	name = slugify(filename.split('.')[0])
	title = instance.ugallery.title
	title = slugify(title)
	return "%s/%s/%s/%s.%s" % (directory, instance.ugallery.owner, title, name, extension)
class UserGallery(models.Model):
	owner = models.ForeignKey(User, related_name="fbla_galo")
	title = models.CharField(max_length=100)
	description = models.TextField()
	shared = models.BooleanField(default=True)
	slug = models.SlugField(blank=True)
	date_added = models.DateTimeField(default=datetime.now())
	class Meta:
		verbose_name_plural = "Galleries"
	class Admin:
		pass
	def __unicode__(self):
		return self.title
	@property
	def latest_photo(self):
		photo = UserGalleryImages.objects.filter(ugallery=self).order_by('-date_added')[0]
		return photo
	@property
	def photo_count(self):
		count = UserGalleryImages.objects.filter(ugallery=self).count()
		return u"%s" % count
	@property
	def ret_thumb(self):
		photo = UserGalleryImages.objects.filter(ugallery=self).order_by('-date_added')[:3]
		return photo
	@property
	def ret_recent(self):
		photo = UserGalleryImages.objects.all().order_by('-date_added')[:4]
		return photo
	def save(self, **kwargs):
		self.slug = slugify(self.title, instance=self)
		super(UserGallery, self).save(**kwargs)
	def delete(self, **kwargs):
		path = '/home/eport/public_html/fbla/fblaproj/usergallery/' + self.owner.username + '/' + slugify(str(self.title))
		images = UserGalleryImages.objects.filter(ugallery=self)
		for i in images:
			i.delete()
		try:
			os.rmdir(path)
		except os.error:
			pass
		super(UserGallery, self).delete(**kwargs)
class UserGalleryImages(models.Model):
	owner = models.ForeignKey(User, related_name="fbla_imageo", blank=True, null=True)
	ugallery = models.ForeignKey(UserGallery, blank=True, related_name="fbla_rgal")
	title = models.CharField(max_length=100)
	description = models.TextField()
	slug = models.SlugField(blank=True)
	image = models.ImageField(upload_to=img_path)
	date_added = models.DateTimeField(default=datetime.now())
	class Meta:
		verbose_name_plural = "Gallery Images"
	class Admin:
		pass
	def __unicode__(self):
		return self.title
	def get_galleries(self):
		return self.ugallery.filter(shared=True)
	def ret_recent(self):
		photo = UserGalleryImages.objects.all().order_by('-date_added')[:4]
		return photo
	def save(self, **kwargs):
		self.slug = slugify(self.title, instance=self)
		self.owner = self.ugallery.owner
		super(UserGalleryImages, self).save(**kwargs)
	def delete(self, **kwargs):
		path = '/home/eport/public_html/fbla/fblaproj/' + str(self.image)
		try:
			os.remove(path)
		except os.error:
			pass
		super(UserGalleryImages, self).delete(**kwargs)
